package es;

import hadoop.utils.FasterJsonTool;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiss
 * @create 2018-04-17 11:30
 **/
public class EsTest {
    private static TransportClient client;
    private static IndexRequest source;

    public static void before() throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        map.put("cluster.name","elasticsearch");

        Settings settings = Settings.builder().put(map).build();
        client = new PreBuiltTransportClient(settings).
                addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),Integer.parseInt("9300")));

    }

    public static void main(String[] args) throws Exception{
        before();
        GetResponse getFields = client.prepareGet("mytest", "mytype", "1").execute().actionGet();
        System.out.println(getFields.getSourceAsString());
        System.out.println("-----------------");
        List<DiscoveryNode> discoveryNodes = client.connectedNodes();
        System.out.println(FasterJsonTool.writeValueAsString(discoveryNodes));

    }


}
